﻿package MyUI{
	import flash.display.*;
	public class MyContainer extends Sprite{
		public var layout = 'vertical';
		public var paddingH:Number = 0;
		public var paddingV:Number = 0;
		public var high:Number = 0,wide:Number = 0;
		public var colPoint:int = 0,rowPoint:int = 0;
		private function _append(child):void{
			if(layout == 'vertical'){
				child.y = high + paddingV;
			}else if(layout == 'horizontal'){
				child.x = wide + paddingH;
			}else{
				var row = layout[rowPoint];
				if(row is Array){
					child.y = colPoint == 0 ? high + paddingV : getChildAt(numChildren - 1).y;
					child.x = row[colPoint];
					colPoint++;
					if(colPoint >= row.length){
						colPoint = 0;
						rowPoint++;
					}
				}else{
					child.y = high + paddingV;
					child.x = row;
					rowPoint++;
				}
				rowPoint = rowPoint >= layout.length ? 0 : rowPoint;
			}
			addChild(child);
			var w:Number = child.x + (child.hasOwnProperty('wide') ? child.wide : child.width);
			var h:Number = child.y + (child.hasOwnProperty('high') ? child.high : child.height);
			wide = wide < w ? w : wide;
			high = high < h ? h : high;
		}
		public function append(...sub):void{
			for(var i:int = 0;i < sub.length;i++){
				_append(sub[i]);
			}
		}
		public function clear(){
			for(var i:int = numChildren - 1;i >= 0;i--){
				removeChildAt(i);
			}
			wide = 0;
			high = 0;
		}
	}
}